﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace KD.Sms.UI_Operate
{
    public partial class frmOutPut : Form
    {
        public frmOutPut()
        {
            InitializeComponent();
        }

        private string E_PackingPath = string.Empty;
        private DataTable E_Source = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Chose":
                    FolderBrowserDialog dialog = new FolderBrowserDialog();
                    dialog.Description = "请选择文件路径";
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        E_PackingPath = dialog.SelectedPath + string.Format("\\{0}", string.IsNullOrEmpty(CacheMemoryCenter.E_DeviceName) ? "导出文件夹" : CacheMemoryCenter.E_DeviceName);
                        txtPath.Text = E_PackingPath;
                    }
                    break;
                case "Query":
                    E_Source = DAL.SqlLite.Main.Instance.DoQuerySamplingForUpload(true);
                    gridMain.DataSource = E_Source;
                    break;
                case "OutPut":
                    DoOutput();
                    break;
            }
        }

        private void frmOutPut_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 
        /// </summary>
        private void DoOutput()
        {
            if (E_Source == null || E_Source.Rows.Count < 1)
            {
                UI.MsgBox.Show(UI.MsgType.Waring, "当前无任何数据需要导出!");
                return;
            }
            if (string.IsNullOrEmpty(E_PackingPath))
            {
                UI.MsgBox.Show(UI.MsgType.Waring, "请选择导出文件存储目录!");
                return;
            }
            //step1:先创建导出目录
            string _strPicturCopyPath = E_PackingPath + "\\Images";
            if (!(System.IO.Directory.Exists(E_PackingPath)))
                System.IO.Directory.CreateDirectory(E_PackingPath);
            if (!(System.IO.Directory.Exists(_strPicturCopyPath)))
                System.IO.Directory.CreateDirectory(_strPicturCopyPath);
            //step2：将数据导出json
            string _strData = DesEncrypt.Encrypt(JsonConvert.SerializeObject(E_Source));
            string _strDataPath = E_PackingPath + string.Format("\\采样数据备份{0}.mkd", DateTime.Now.ToString("yyMMdd"));
            if (System.IO.File.Exists(_strDataPath))
                System.IO.File.Delete(_strDataPath);
            System.IO.TextWriter tw = new System.IO.StreamWriter(_strDataPath, true);
            tw.WriteLine(_strData);
            tw.Close();
            //step3:复制图片文件

            string _strImgPathLocal, _strImgPathTarget;
            foreach (DataRow dr in E_Source.Rows)
            {
                _strImgPathLocal = dr["Img_Begin"] == DBNull.Value ? string.Empty : dr["Img_Begin"].ToString();
                _strImgPathTarget = _strPicturCopyPath + "\\" + dr["Sampling_id"].ToString() + "Before.jkdg";
                DoCopyFile(_strImgPathLocal, _strImgPathTarget);
                _strImgPathLocal = dr["Img_End"] == DBNull.Value ? string.Empty : dr["Img_End"].ToString();
                _strImgPathTarget = _strPicturCopyPath + "\\" + dr["Sampling_id"].ToString() + "After.jkdg";
                DoCopyFile(_strImgPathLocal, _strImgPathTarget);
            }
            MessageBox.Show("数据导出完毕，请将整个导出目录复制!");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pathLocal"></param>
        /// <param name="pathTarget"></param>
        private void DoCopyFile(string pathLocal, string pathTarget)
        {
            if (!string.IsNullOrEmpty(pathLocal) && !string.IsNullOrEmpty(pathTarget))
            {
                if (File.Exists(pathLocal))
                    File.Copy(pathLocal, pathTarget);
            }
        }



    }
}
